package com.ianmoone.JZOF;

import java.math.BigInteger;
import java.nio.channels.NonWritableChannelException;
import java.util.Scanner;

public class STKJTest1 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		Stack<Integer> stack = new Stack<>(); 
		int k = in.nextInt();
		int[] num = new int[n];
		in.nextLine();
		String string = in.nextLine();
		String[] split = string.split(" ");
		long sum = 0;
		for (int i = 0; i < n; i++) {
			num[i] = Integer.valueOf(split[i]);
			 sum += num[i];
		}
		long a = sum/n;
		int min = Integer.MAX_VALUE;
		int max = Integer.MIN_VALUE;
		for (int i = 0; i < n ; i++) {
			if (num[i]>=a) {
				num[i] -= k;
			}else {
				num[i] += k;
			}
			min = min < num[i] ? min : num[i];
			max = max > num[i] ? max : num[i];
		}
		System.out.println(max - min);
	}
}
